/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import stateful.ChatSessionLocal;
/**
 *
 * @author oana.leva
 */
public class SignUpServlet extends HttpServlet {

    @EJB private ChatSessionLocal chatEJB;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet LogInServlet</title>");
        out.println("</head>");
        out.println("<body>");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String desc = request.getParameter("description");

        if (chatEJB.SignUserUp(name, pwd, desc))
        {
            if (chatEJB.LogUserIn(name, pwd))
            {
                out.println( "hello" +name + "<br>");
                out.println("<a href='MainPage.html'>Go to main page</a>");
            }
            else
            {
                out.println("login error!");
            }
        }
        else
        {
            out.println("Sign up failed. <br>Please <a href='SignUpForm.html'>try again</a> with a different user name");
        }
        out.println("</body>");
        out.println("</html>");
    }

}